package com.example.mytest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TAG = "ActivityDemo";
    private static FileOutputStream outStream ;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Button button = (Button) findViewById(R.id.button1);
		final TextView textView = (TextView) findViewById(R.id.textView1);
		final EditText editText = (EditText) findViewById(R.id.editText1);

		try {
			FileInputStream inStream = this.getBaseContext().openFileInput(
					"log.txt");
			String strLog = Comm.readInStream(inStream);
			textView.setText(strLog);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			outStream = this.openFileOutput("log.txt", Context.MODE_APPEND);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		OnClickListener clickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String input = editText.getText().toString();
				textView.setText(input);

				try {
					if (outStream != null) {
						outStream.write(input.getBytes());
						outStream.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		};

		button.setOnClickListener(clickListener);

		Log.i(TAG, "start onCreate~~~");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.i(TAG, "start onStart~~~");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.i(TAG, "start onRestart~~~");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "start onResume~~~");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.i(TAG, "start onPause~~~");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.i(TAG, "start onStop~~~");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i(TAG, "start onDestroy~~~");
	}
}
